package com.yimixian.app.cart;

import com.yimixian.app.R;
import com.yimixian.app.common.Constants;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.wheelview.BaseFragmentFrameLayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CartGoodsItemView extends BaseFragmentFrameLayout implements GoodsItemPresenter {

    @InjectView(R.id.name_text)
    TextView mNameText;
    @InjectView(R.id.price_text)
    TextView mPriceText;
    @InjectView(R.id.quantity_text)
    TextView mQuantityText;
    @InjectView(R.id.plus_icon)
    ImageView mPlusButton;
    @InjectView(R.id.minus_icon)
    ImageView mMinusButton;
    @InjectView(R.id.item_container)
    View mItemContainer;
    @InjectView(R.id.divider_view)
    View mDividerView;

    protected GoodsItem mGoodsItem;
    protected GoodsItemPresenter.ButtonListener mButtonListener;
    protected int mQuantity;

    public CartGoodsItemView(Context context) {
        super(context);
        initCartGoodsItemView();
    }

    public CartGoodsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCartGoodsItemView();
    }

    public CartGoodsItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initCartGoodsItemView();
    }

    protected void initCartGoodsItemView() {
        init();
        mQuantity = 0;
        mPlusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mButtonListener != null) {
                    mButtonListener.onPlusButtonClicked(mGoodsItem.id);
                }
            }
        });
        mMinusButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mButtonListener != null) {
                    mButtonListener.onMinusButtonClicked(mGoodsItem.id);
                }
            }
        });

    }

    protected void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.cart_goods_item_view, this);
        ButterKnife.inject(this);
    }

    public void bind(GoodsItem goodsItem) {
        mGoodsItem = goodsItem;
        if (mGoodsItem == null) {
            return;
        }

        mNameText.setText(goodsItem.name);
    }

    public void setShowDividerBelow(boolean value) {
        mDividerView.setVisibility(value ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void setQuantity(int quantity) {
        mQuantity = quantity;
        if (mGoodsItem.typeGift == null) {
            boolean canBuy = quantity < mGoodsItem.shouldBuyCount;
            mPlusButton.setClickable(canBuy);
            mPlusButton.setImageResource(canBuy ? R.drawable.cart_plus_icon_new
                    : R.drawable.cart_plus_unavailable_icon);
        }
        boolean canReduce = quantity > 0;
        mMinusButton.setClickable(canReduce);
        mMinusButton.setImageResource(canReduce ? R.drawable.cart_minus_icon
                : R.drawable.cart_minus_unavailable_icon);
        mItemContainer.setAlpha(quantity > 0 ? 1f : .4f);
        mQuantityText.setText(String.valueOf(quantity));
        mPriceText.setText(Constants.YUAN + " " +
                String.format("%.2f", Float.valueOf(mGoodsItem.unitPrice)));
    }

    public void updatePlusIconByTotalPriceAndFreebieBoughtFlag(float totalPrice,
            boolean freebieBoughtFlag) {
        GoodsItem.GoodsGiftType typeGift = mGoodsItem.typeGift;
        if (typeGift != null) {
            if (freebieBoughtFlag || typeGift.minTotalPrice > totalPrice
                    || mGoodsItem.shouldBuyCount < 1) {
                mPlusButton.setImageResource(R.drawable.sellbox_add_turngreg);
                mPlusButton.setClickable(false);
            } else {
                mPlusButton.setImageResource(R.drawable.sellbox_add);
                mPlusButton.setClickable(true);
            }
        }

    }

    @Override
    public void setButtonListener(ButtonListener listener) {
        mButtonListener = listener;
    }

    @Override
    public int getGoodsItemId() {
        return mGoodsItem.id;
    }
}
